package com.jokerku.lottery.domain.receive.filter.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jokerku.lottery.domain.receive.filter.EventFilter;
import com.jokerku.lottery.domain.receive.model.BehaviorMatter;
import com.jokerku.lottery.domain.receive.model.MessageTextEntity;
import com.jokerku.lottery.infrastructure.utils.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: guzq
 * @CreateTime: 2022/08/31 10:29
 * @Description: 抽奖事件
 * @Version: 1.0
 */
@Service("lottery")
public class LotteryFilter implements EventFilter {

    private static final Map<String, String> CITY_TO_WEATHER_MAP = new ConcurrentHashMap<>();
    private static final List<String> USER_OPEN_ID_LIST = new CopyOnWriteArrayList<>();
    private static final List<String> WHITE_LIST = Arrays.asList("oBnB75m7smrocczUcndu3e9dvGqQ");

    @Override
    public String filter(BehaviorMatter req) {
      return getWeatherByCityName(req.getOpenId(), req.getContent());
    }

    private String getWeatherByCityName(String userOpenId, String cityName) {
        String response = CITY_TO_WEATHER_MAP.get(cityName);
        if (StringUtils.isNotBlank(response)) {
            return response;
        }

//        if (userOpenIdList.contains(userOpenId) && !whitelist.contains(userOpenId)) {
//            response = "(＞人＜；)对不起！今日次数已用完😙";
//            return response;
//        }

        if (StringUtils.equalsAnyIgnoreCase(cityName, "jp","精品")) {
            response = "👮包吃包住包玩👮";
            return response;
        }


        if (cityName.endsWith("市")) {
            cityName = cityName.substring(0, cityName.length() - 1);
        }

        String url = "https://v0.yiketianqi.com/api";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("unescape", 1);
        paramMap.put("version", "v61");
        paramMap.put("appid", "13195123");
        paramMap.put("appsecret", "9GGxBHjn");
        paramMap.put("city", cityName);
        response = HttpUtil.get(url, paramMap, 10000);

        JSONObject jsonObject = JSON.parseObject(response);
        if (StringUtils.isBlank(jsonObject.getString("city"))) {
            response = "(＞人＜；)对不起！暂未查询到该城市😙";
            return response;
        }

        response = String.format("💐💐 %s当前气温：%s ℃，最高温：%s ℃，最低温：%s ℃，湿度：%s，风向：%s，风力等级：%s，风速：%s 💐💐",
                jsonObject.get("city"), jsonObject.get("tem"), jsonObject.get("tem1"), jsonObject.get("tem2"),
                jsonObject.get("humidity"), jsonObject.get("win"), jsonObject.get("win_speed"), jsonObject.get("win_meter"));

        USER_OPEN_ID_LIST.add(userOpenId);

        return response;
    }
}
